package com.lanou.bookstore.book.dao;

import com.lanou.bookstore.book.domain.Book;
import com.lanou.bookstore.category.domain.CBook;
import com.lanou.bookstore.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookDao {
    public Book load(String bid) {

        String sql = "SELECT * FROM book WHERE bid=?";
        Connection conn = JDBCUtil.getConn();
        try {
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, bid);
            ResultSet rs = statement.executeQuery();
            rs.next();
            Book book = new Book();
            setBookParam(book, rs);

            rs.close();
            statement.close();
            return book;

        } catch (SQLException e) {
           throw new RuntimeException(e);
        }

    }





    private void setBookParam(Book Book, ResultSet rs) throws SQLException {
        String bid = rs.getString("bid");
        String cid = rs.getString("cid");
        String bname = rs.getString("bname");
        Float price = rs.getFloat("price");
        String author = rs.getString("author");
        String img = rs.getString("image");
        Book.setPrice(price).setAuthor(author).setBname(bname).setImage(img).setBid(bid).setCid(cid);


    }
}